unit UCadEmpresas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Modelo, StdCtrls, Mask, DBCtrls, DB, DBTables, Grids, DBGrids,
  Buttons, CheckLst, ExtCtrls, ExtDlgs, UDPTOUteis, ComCtrls, JPEG;

type
  TFCadEmpresas = class(TFModelo)
    intgrfldPadraoEMPCODIGO: TIntegerField;
    qPadraoEMPRAZAOSOCIAL: TStringField;
    intgrfldPadraoCIDCODIGO: TIntegerField;
    qPadraoEMPFANTASIA: TStringField;
    qPadraoENDCEP: TStringField;
    qPadraoEMPEMAIL: TStringField;
    qPadraoEMPHOMEPAGE: TStringField;
    qPadraoEMPLOGRADOURO: TStringField;
    qPadraoEMPNUMERO: TStringField;
    qPadraoEMPCNPJ: TStringField;
    qPadraoEMPIE: TStringField;
    qPadraoEMPBAIRRO: TStringField;
    qPadraoEMPCOMPLEMENTO: TStringField;
    qPadraoEMPDTCADASTRO: TDateTimeField;
    qPadraoEMPSTATUS: TStringField;
    qPadraoEMPLOGOTIPO: TBlobField;
    dlgLogo: TOpenPictureDialog;
    pgc1: TPageControl;
    tsEmpresa: TTabSheet;
    Lb1: TLabel;
    Lb2: TLabel;
    Lb3: TLabel;
    Lb4: TLabel;
    Lb5: TLabel;
    Lb6: TLabel;
    Lb7: TLabel;
    Lb8: TLabel;
    Lb9: TLabel;
    Lb10: TLabel;
    Lb11: TLabel;
    Lb12: TLabel;
    Lb14: TLabel;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBEdit3: TDBEdit;
    DBEdit4: TDBEdit;
    DBEdit5: TDBEdit;
    DBEdit7: TDBEdit;
    DBEdit8: TDBEdit;
    DBEdit9: TDBEdit;
    DBEdit10: TDBEdit;
    DBEdit11: TDBEdit;
    DBEdit12: TDBEdit;
    DBEdit13: TDBEdit;
    DBEdit14: TDBEdit;
    Lb15: TLabel;
    DBEdit15: TDBEdit;
    tsLogo: TTabSheet;
    qPadraoEMPTELEFONE: TStringField;
    dbedtEMPTELEFONE: TDBEdit;
    Lb16: TLabel;
    EdDescCidade: TEdit;
    imgLogoEmpresa: TImage;
    procedure DBEdit4DblClick(Sender: TObject);
    procedure DBEdit4Exit(Sender: TObject);
    procedure BtNovoClick(Sender: TObject);
    procedure qPadraoAfterInsert(DataSet: TDataSet);
    procedure DBEdit2Exit(Sender: TObject);
    procedure DBEdit10Exit(Sender: TObject);
    procedure qPadraoAfterScroll(DataSet: TDataSet);
    procedure imgLogoEmpresaDblClick(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure qPadraoAfterCancel(DataSet: TDataSet);
    procedure qPadraoAfterDelete(DataSet: TDataSet);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FCadEmpresas: TFCadEmpresas;

implementation

uses UPesqEnderecoCEP;

{$R *.dfm}

procedure TFCadEmpresas.DBEdit4DblClick(Sender: TObject);
begin
  inherited;
  DBEdit4.Text := DPTOChamaPesquisa(FPesqEndereco,TFPesqEndereco,'ENDCEP', QPadrao, DBEdit4.Text);
end;

procedure TFCadEmpresas.DBEdit4Exit(Sender: TObject);
var cid, log, bai :string;
begin
  inherited;
  IF DBEdit4.Text <> '' then
    begin
      if ((qPadrao.state <> dsEdit) and (qPadrao.State <> dsInsert)) then
           qPadrao.Edit;

      DPTOBuscaEndereco(qPadrao.FieldByName('ENDCEP').ASsTRING , cid, log, bai);
      //
      qPadrao.FieldByName('CIDCODIGO').AsInteger    := StrToInt(Cid);
      qPadrao.FieldByName('EMPLOGRADOURO').AsString := log;
      qPadrao.FieldByName('EMPBAIRRO').AsString     := bai;
      //
      if DBEdit2.Text <> '' then
        Begin
          qGenerica.Close;
          qGenerica.SQL.Clear;
          qGenerica.SQL.Add('select CIDDESCRICAO from cidade where CIDCODIGO = ' + QuotedStr(DBEdit2.Text));
          qGenerica.Open;
          EdDescCidade.Text := qGenerica.fieldByName('CIDDESCRICAO').AsString;
        end;
      //
      DBEdit9.SetFocus;
    end;
end;

procedure TFCadEmpresas.BtNovoClick(Sender: TObject);
begin
  inherited;
  pgc1.TabIndex := 0;
  DBEdit1.SetFocus;
end;

procedure TFCadEmpresas.qPadraoAfterInsert(DataSet: TDataSet);
begin
  inherited;
  qPadrao.FieldByName('EMPCODIGO').AsInteger := DPTOUltimo('EMPRESA', 'EMPCODIGO','');
  qPadrao.FieldByName('EMPDTCADASTRO').AsDateTime := StrToDate( FormatDateTime('dd/MM/yyyy', DPTODataEHora) );
end;

procedure TFCadEmpresas.DBEdit2Exit(Sender: TObject);
begin
  inherited;
  if DBEdit2.Text <> '' then
  Begin

    qGenerica.Close;
    qGenerica.SQL.Clear;
    qGenerica.SQL.Add('select CIDDESCRICAO from cidade where CIDCODIGO = ' + QuotedStr(DBEdit2.Text));
    qGenerica.Open;
    EdDescCidade.Text := qGenerica.fieldByName('CIDDESCRICAO').AsString;
  end;
  qGenerica.Close;

end;

procedure TFCadEmpresas.DBEdit10Exit(Sender: TObject);
begin
  inherited;
  if (DBEdit10.Text<>'') and not (ValidaCNPJ(DBEdit10.Text)) then
    begin
      ShowMessage('CNPJ invalido. Verifique!');
      DBEdit10.SetFocus;
    end;
end;

procedure TFCadEmpresas.qPadraoAfterScroll(DataSet: TDataSet);
begin
  inherited;
  if DBEdit2.Text <> '' then
  Begin
    qGenerica.Close;
    qGenerica.SQL.Clear;
    qGenerica.SQL.Add('select CIDDESCRICAO from cidade where CIDCODIGO = ' + QuotedStr(DBEdit2.Text));
    qGenerica.Open;
    EdDescCidade.Text := qGenerica.fieldByName('CIDDESCRICAO').AsString;
  end;
  //
  DPTOMostraImgNoTImage(qPadrao, 'EMPLOGOTIPO', imgLogoEmpresa);
  //
  qGenerica.Close;
end;

procedure TFCadEmpresas.imgLogoEmpresaDblClick(Sender: TObject);
begin
  inherited;
  //
  if dlgLogo.Execute then
  BEGIN
    qPadrao.Edit;
    DPTOSalvaImgNoBanco(qPadrao, 'EMPLOGOTIPO', dlgLogo.FileName, imgLogoEmpresa);
  end;
  //
end;

procedure TFCadEmpresas.FormActivate(Sender: TObject);
begin
  inherited;
  pgc1.TabIndex := 0;
end;

procedure TFCadEmpresas.qPadraoAfterCancel(DataSet: TDataSet);
begin
  inherited;
  EdDescCidade.Text := '';
end;

procedure TFCadEmpresas.qPadraoAfterDelete(DataSet: TDataSet);
begin
  inherited;
  EdDescCidade.Text := '';
end;

end.
